jQuery["post"] = function( url, data, callback, type ) {
    type = type || "json";
    if ( jQuery.isFunction( data ) ) {
        type = type || callback;
        callback = data;
        data = undefined;
    }
    return jQuery.ajax({
        type: "post",
        url: url,
        data: data,
        success: callback,
        dataType: type
    });
};
$(document).ready(function(){
    /*setTimeout(function() {
     if(window.pageYOffset !== 0) return;
     window.scrollTo(0, window.pageYOffset + 1);
     }, 1000);*/
    //计算body高度
    if($("body").height() < $(window).height()){$("body").height($(window).height());}
    //图片懒加载，针对图片轮播
    $("div.swipe img").each(function(){
        var _self = $(this);setTimeout(function(){_self.attr("src",_self.attr("lazysrc"));},50);
    });
    //图片懒加载
    $("img.lazysrc").each(function(){
        if( $(window).height() > $(this).offset().top){
            $(this).attr("src",$(this).attr("lazysrc"));
        }
        $(this).one("appear",function(){
            $(this).attr("src",$(this).attr("lazysrc"));
        });
    });
    // 下拉菜单选择
    $("#menu").on('click',function(){
        $("#langlist").removeClass("show");
        if($("#menulist").css("display") == "none"){
            $("#menulist").addClass("show");
            if($("#container").height() < $("#menulist").height()){
                $("#container").height($("#menulist").height());
            }
        }else{
            $("#menulist").removeClass("show");
        }
        $('#toTop_div').css("display", "none");
        return false;
    });
    $("#menulist").on("click",function(){
        $("#menulist").removeClass("show");
    });
    // 语言选择
    $("#lang").on('click',function(){
        $("#menulist").removeClass("show");
        if($("#langlist").css("display") == "none"){
            $("#langlist").addClass("show");
        }else{
            $("#langlist").removeClass("show");
        }
        $('#toTop_div').css("display", "none");
        return false;
    });
    $("#langlist").on("click",function(){
        $("#langlist").removeClass("show");
    });


    $("#page").bind("click",function(e){
        $('#toTop_div').css("display", "none");
        //if($("#menulist").css("display") == "block" && e.srcElement.className.indexOf("menu-item") ==-1){
            $("#menulist").removeClass("show");
        //}
        //if($("#langlist").css("display") == "block" && e.srcElement.className.indexOf("menu-item") ==-1){
            $("#langlist").removeClass("show");
        //}
        //if($(".dropdown-menu-top").css("display") == "block" && e.srcElement.className.indexOf("menu-item") ==-1){
            $(".dropdown-menu-top").removeClass("show");
        //}
        return true;
    });
    $ (window).bind("scroll",function(){
        //图片懒加载
        $("img.lazysrc").each(function(){
            var fold = $(window).height() + $(window).scrollTop()+200;
            if( fold >= $(this).offset().top){
                $(this).trigger("appear");
            }
        });
    });

});

function show_action(){
    $("#menulist").removeClass("show");
    if($('#toTop_div').css("display") == "none"){
        $('#toTop_div').css("display", "block");
    }else{
        $('#toTop_div').css("display", "none");
    }
    var content = window.location.href;
    $('#qrcode').empty();
    $('#qrcode').qrcode({width:180,height:180,correctLevel:0,text:content});
    window.event.cancelBubble = true;
}

var appUtil = {
    setCookie:function(name,value,options){
        appUtil.delCookie(name);
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    },getCookie:function(name){
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookieStr = jQuery.trim(cookies[i]);
                var reg = new RegExp("^"+name+"=","ig");
                if(reg.test(cookieStr)){
                    cookieValue = decodeURIComponent(cookieStr.replace(reg,""));
                }

            }
        }
        return cookieValue;
    },delCookie:function(name,options){
        options = options || {};
        var path = options.path ? '; path=' + (options.path) : '/';
        document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" + path;
    },alert:function(msg){
        var self = this;
        if(!this.msg_back){
            var backHtml = '<div id="msg_back" onclick="appUtil.hideAlert();" style="display:none;position:absolute;background-color: #666;width:100%;height:100%;'+
                'left:0px;top:0px;filter:alpha(opacity=10);opacity:0.1;z-index:80;"></div>';
            var bodyHtml = '<div id="msg_body" onclick="appUtil.hideAlert();" style="display:none;position:fixed;overflow:hidden;border-radius:5px;'+
                'text-align:center;z-index:81;width:90%;color:#FFF;height:50px;line-height:50px;top:50px;left:50%;'+
                'margin-left:-45%;filter:alpha(opacity=60);opacity: 0.8;background:#000;"></div>';
            this.msg_back = $(backHtml);
            this.msg_body = $(bodyHtml);
            this.msg_back.appendTo(document.body);
            this.msg_body.appendTo(document.body);
        }
        this.msg_body.html(msg);
        self.msg_back.show();
        self.msg_body.show();
        if($.browser.safari && $.browser.version <= 532.9 ){
            window.scrollTo(0,0);
        }
        msgTimeout = setTimeout(function(){
            self.msg_back.hide();
            self.msg_body.hide();
        },2000);
    },hideAlert:function(){
        this.msg_back.hide();
        this.msg_body.hide();
        if(msgTimeout){window.clearTimeout(msgTimeout);}
    },openShare:function(){// 打开分享
        if($("#share").length == 0){
            this.shareInfo = this.shareInfo || {};
            this.shareInfo.title = this.shareInfo.title || document.title||"";
            this.shareInfo.desc = this.shareInfo.desc || $("meta[name=Description]").attr("content");
            this.shareInfo.images = this.shareInfo.images || fullctx + $("#brand > img").attr("lazysrc") || $("#image img:first").attr("lazysrc") || $(".article-content img:first").attr("lazysrc");
            this.shareInfo.url = this.shareInfo.url || window.location.href;
            var html = [];
            html[html.length] = '<div class="share_box_bg" id="sharebg" onclick="appUtil.openShare();"></div>';
            html[html.length] = '<div id="share" class="share_box">';
            html[html.length] = '    <div class="share_box_tit"><span class="close" onclick="appUtil.openShare();"></span>分享到</div>';
            html[html.length] = '    <div class="box-btn phone-btn">';
            html[html.length] = '    <a id="share_to_sms"  class="button big" href="#">';
            html[html.length] = '        <img style="position:absolute;left:0px;" src="'+ctx+'/site/images/fx_smsg.png" align="absmiddle">分享到手机短信';
            html[html.length] = '        </a>';
            html[html.length] = '    </div>';
            html[html.length] = '    <div class="box-btn phone-btn">';
            html[html.length] = '    <a id="share_to_sina_wb"  class="button big" target="_blank" href="#">';
            html[html.length] = '        <img style="position:absolute;left:0px;" src="'+ctx+'/site/images/fx_weibo.png" align="absmiddle">分享到新浪微博';
            html[html.length] = '        </a>';
            html[html.length] = '    </div>';
            html[html.length] = '    <div class="box-btn phone-btn" >';
            html[html.length] = '    <a id="share_to_tencent_wb"  class="button big " target="_blank" href="#">';
            html[html.length] = '        <img style="position:absolute;left:0px;" src="'+ctx+'/site/images/fx_tweibo.png" align="absmiddle">分享到腾讯微博';
            html[html.length] = '        </a>';
            html[html.length] = '    </div>';
            html[html.length] = '    <div class="box-btn phone-btn" style="margin-bottom:15px;">';
            html[html.length] = '    <a id="share_to_qqzone" class="button big " target="_blank" href="#" >';
            html[html.length] = '        <img style="position:absolute;left:0px;" src="'+ctx+'/site/images/fx_qqzone.png" align="absmiddle">分享到QQ空间';
            html[html.length] = '        </a>';
            html[html.length] = '    </div>';
            html[html.length] = '    </div>';
            $("body").append(html.join(""));
            if(!this.shareInfo.shortUrl){
                $.post(ctx+"/site/site.action.jsp", {"action": "getShortUrl", "url": this.shareInfo.url}, function (result) {
                    if(result.isSuccess){
                        appUtil.shareInfo.shortUrl = result.data;
                        appUtil.createLink();
                        $("#share").show();
                        $("#sharebg").show();
                    }
                });
            }else{
                appUtil.createLink();
            }
        }else{
            if($("#share").css("display") == "block"){
                $("#share").hide();
                $("#sharebg").hide();
            }else{
                $("#share").show();
                $("#sharebg").show();
            }
        }
        this.createLink = function(){
            var p = {
                url:appUtil.shareInfo.shortUrl,
                web_url:appUtil.shareInfo.shortUrl,
                burl:appUtil.shareInfo.shortUrl,
                showcount:'1',/*是否显示分享总数,显示：'1'，不显示：'0' */
                summary:encodeURIComponent(appUtil.shareInfo.desc),/*分享摘要(可选)*/
                title:encodeURIComponent(appUtil.shareInfo.title),/*分享标题(可选)*/
                site:encodeURIComponent('宇鑫互联'),/*分享来源 如：腾讯网(可选)*/
                pic_url:appUtil.shareInfo.images /*分享图片的路径(可选)*/
            };
            var s = [];
            for(var i in p){
                s.push(i + '=' + (p[i]||''));
            }
            $("#share_to_sms").attr("href","sms:?body="+encodeURIComponent(appUtil.shareInfo.title)+appUtil.shareInfo.shortUrl);
            $("#share_to_sina_wb").attr("href","http://service.weibo.com/share/share.php?title="+encodeURIComponent(appUtil.shareInfo.title)+appUtil.shareInfo.url);
            $("#share_to_tencent_wb").attr("href","http://share.v.t.qq.com/index.php?c=share&a=index&title="+encodeURIComponent(appUtil.shareInfo.title)+appUtil.shareInfo.shortUrl);
            $("#share_to_qqzone").attr("href",'http://sns.z.qq.com/share?'+s.join('&'));
        }
    }
}

/*** 打开更多分类 ***/
function openMore(){
    $("#page").hide();
    $("#more-class").show();
}
/*** 返回列表 ***/
function backList(){
    $("#page").show();
    $("#more-class").hide();
}